package com.aliengen.thingple.driverapp.network.volley;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public abstract class ResponseErrorHandler implements Response.ErrorListener {

    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        Log.e("ResponseErrorHandler", "Network error");
        if(error instanceof TimeoutError)
        {
            Log.e("ResponseErrorHandler", "Timeout");
            onError("Request timeout!");
            return;
        }

        if(error instanceof ServerError)
        {
            Log.e("ResponseErrorHandler", new String(error.networkResponse.data));
            onError("Unexpected response from server!");
            return;
        }

        if(error instanceof AuthFailureError)
        {
            Log.e("ResponseErrorHandler", new String(error.networkResponse.data));
            onError("Unable to authenticate the request to the server!");
            return;
        }

        onError(error.getMessage());
    }

    public abstract void onError(String error);
}
